package com.ruoyi.receipt.controller;

import java.util.List;

import com.ruoyi.receipt.domain.TbExampleSubjectManager;
import com.ruoyi.receipt.domain.TbKnowledgeManager;
import com.ruoyi.receipt.domain.TbSubjectCluste;
import com.ruoyi.receipt.service.ITbExampleSubjectManagerService;
import com.ruoyi.receipt.service.ITbKnowledgeManagerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.receipt.domain.TbExampleSubjectAnalysis;
import com.ruoyi.receipt.service.ITbExampleSubjectAnalysisService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 例题解析Controller
 * 
 * @author ruoyi
 * @date 2021-04-01
 */
@RestController
@RequestMapping("/receipt/exampleSubjectAnalysis")
public class TbExampleSubjectAnalysisController extends BaseController
{
    @Autowired
    private ITbExampleSubjectAnalysisService tbExampleSubjectAnalysisService;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private ITbKnowledgeManagerService tbKnowledgeManagerService;
    @Autowired
    private ITbExampleSubjectManagerService tbExampleSubjectManagerService;
    /**
     * 查询例题解析列表
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectAnalysis:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbExampleSubjectAnalysis tbExampleSubjectAnalysis)
    {
        startPage();
        List<TbExampleSubjectAnalysis> list = tbExampleSubjectAnalysisService.selectTbExampleSubjectAnalysisList(tbExampleSubjectAnalysis);
        for (int i=0;i<=list.size()-1;i++){
            TbExampleSubjectAnalysis tbExampleSubjectAnalysis1 = list.get(i);
            TbKnowledgeManager tbKnowledgeManager = tbKnowledgeManagerService.selectTbKnowledgeManagerById(tbExampleSubjectAnalysis1.getKnowledgeManagerRelated());
            tbExampleSubjectAnalysis1.setZsName(tbKnowledgeManager.getTitle());
            TbExampleSubjectManager tbExampleSubjectManager = tbExampleSubjectManagerService.selectTbExampleSubjectManagerById(tbExampleSubjectAnalysis1.getSubjectManagerId());
            tbExampleSubjectAnalysis1.setClusteName(tbExampleSubjectManager.getTitle());
        }
        return getDataTable(list);
    }
    /**
     * 导出例题解析列表
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectAnalysis:export')")
    @Log(title = "例题解析", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbExampleSubjectAnalysis tbExampleSubjectAnalysis)
    {
        List<TbExampleSubjectAnalysis> list = tbExampleSubjectAnalysisService.selectTbExampleSubjectAnalysisList(tbExampleSubjectAnalysis);
        ExcelUtil<TbExampleSubjectAnalysis> util = new ExcelUtil<TbExampleSubjectAnalysis>(TbExampleSubjectAnalysis.class);
        return util.exportExcel(list, "exampleSubjectAnalysis");
    }

    /**
     * 获取例题解析详细信息
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectAnalysis:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        TbExampleSubjectAnalysis tbExampleSubjectAnalysis = tbExampleSubjectAnalysisService.selectTbExampleSubjectAnalysisById(id);
        //根据id 获取绑定的知识点名字
        TbKnowledgeManager tbKnowledgeManager = tbKnowledgeManagerService.selectTbKnowledgeManagerById(tbExampleSubjectAnalysis.getKnowledgeManagerRelated());
        tbExampleSubjectAnalysis.setZsName(tbKnowledgeManager.getTitle());
        //根据id 获取绑定的例题名字
        TbExampleSubjectManager tbExampleSubjectManager = tbExampleSubjectManagerService.selectTbExampleSubjectManagerById(tbExampleSubjectAnalysis.getSubjectManagerId());
        tbExampleSubjectAnalysis.setClusteName(tbExampleSubjectManager.getTitle());
        return AjaxResult.success(tbExampleSubjectAnalysis);
    }
    /**
     * 新增例题解析
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectAnalysis:add')")
    @Log(title = "例题解析", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbExampleSubjectAnalysis tbExampleSubjectAnalysis)
    {
        tbExampleSubjectAnalysis.setId(idWorker.nextId());
        tbExampleSubjectAnalysis.setIsDel(0);
        tbExampleSubjectAnalysis.setStatus(1);
        return toAjax(tbExampleSubjectAnalysisService.insertTbExampleSubjectAnalysis(tbExampleSubjectAnalysis));
    }
    /**
     * 修改例题解析
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectAnalysis:edit')")
    @Log(title = "例题解析", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbExampleSubjectAnalysis tbExampleSubjectAnalysis)
    {
        tbExampleSubjectAnalysis.setStatus(1);
        return toAjax(tbExampleSubjectAnalysisService.updateTbExampleSubjectAnalysis(tbExampleSubjectAnalysis));
    }
    /**
     * 删除例题解析
     */
    @PreAuthorize("@ss.hasPermi('receipt:exampleSubjectAnalysis:remove')")
    @Log(title = "例题解析", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove( @PathVariable Long[] ids) {
        //根据id查询实体
        TbExampleSubjectAnalysis tbExampleSubjectAnalysis = new TbExampleSubjectAnalysis();
        for (int i=0;i<ids.length;i++){
          tbExampleSubjectAnalysis=  tbExampleSubjectAnalysisService.selectTbExampleSubjectAnalysisById(ids[i]);
            tbExampleSubjectAnalysis.setIsDel(1);
            tbExampleSubjectAnalysisService.updateTbExampleSubjectAnalysis(tbExampleSubjectAnalysis);
        }
        return AjaxResult.success();
    }
    /**
     * 获取树结构数据
     * @return
     */
    @GetMapping("/getTreeList")
    public List<TbExampleSubjectManager> getTreeList(){
        List<TbExampleSubjectManager> list = tbExampleSubjectManagerService.selectTbExampleSubjectManagerstatus(new TbExampleSubjectManager());
        return list;
    }
}
